<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/3/27
 * Time: 21:06
 */
namespace app\admin\model;

use think\Model;
use think\Validate;
use think\Config;

class LabelsModel extends  Model
{
    protected $table = 'yui_labels';
    /*
     * rule
     * 验证data
     * @params $data
     * 新增和修改时验证
     * */
    public function validate_model($scene){
        $rule = [
            'name' => 'require|chsDash|max:32|token',
            'category' => 'require|alpha|max:32',
            'sort' => 'number',
            'status' => 'number'
        ];
        $msg = [
            'name.require' => '标签名不能为空!',
            'name.chsDash' => '标签名不合法!',
            'name.max' => '标签名太长!',
            'code.require' => '标签code不能为空!',
            'code.alpha' => '标签code必须为字母!',
            'code.max' => '标签code太长!',
            'category.require' => '标签模块不能为空!',
            'category.alpha' => '标签模块必须为字母!',
            'category.length' => '标签模块太长!',
            'sort.number' => '标签排序必须为数字类型!',
            'status.number' => '标签状态必须为数字类型!',
        ];
        if ($scene=="edit"){
            $rule['code']='require|alpha|max:32';
        }else if ($scene=="add"){
            $rule['code']='unique:labels|require|alpha|max:32';
            $msg['code.unique']='标签code已经存在!';
        }
        $data=input('post.');
        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if(!$result){
            return [
                'code'=>0,
                'text'=> $validate->getError()
            ];
        }
        return ['code'=>1];
    }

    /*
    * 模块获取器
    * category
    * 驼峰式规则
    * */
    public function getCategoryAttr($value){
        $rule=Config::get('labels_category');
        return $rule[$value];
    }

    /*
   * 根据ID获取标签信息
   * */
    public function getById($id){
        return $this->where(["id"=>$id])->find();
    }

    /*
     * 标签列表
     * */
    public function listAll(){
       return $this->order('sort desc')->select();
    }

    /*
     * 标签启用禁用列表
     * */
    public function listOnOff($status){
        $data=$this->where('status',$status)->order('sort desc')->select();
        return $data;
    }

    /*
    * 根据MAP获取信息
    * */
    public function getByMaps($map){
        return $this->where($map)->order('updated_at desc')->select();
    }

    /*
     * 新增标签
     * */
    public function add(){
        $data=input('post.');
        $data['created_at']=time();
        $data['updated_at']=time();
        unset($data['__token__']);
        if($this->save($data)){
            $params=[
                Config::get('op_type')[0],
                Config::get('op_module')[3],
                Config::get('op_text')[4],
                $data['name']
            ];
            tag('logs_db',$params);
            return op_success();
        }
        return op_fail();
    }

    /*
     * 修改标签
     * */
    public function edit(){
        $data=input('post.');
        $data['updated_at']=time();
        unset($data['__token__']);
        $id=input('post.id/d');
        if($this->where('id',$id)->update($data)){
            $params=[
                Config::get('op_type')[1],
                Config::get('op_module')[3],
                Config::get('op_text')[4],
                $data['name']
            ];
            tag('logs_db',$params);
            return op_success();
        }
        return op_fail();
    }

    /*
     * 删除标签
     * */
    public function del(){
        $ids=input('post.ids/a');
        $op_name=[];
        foreach ($ids as $id){
            array_push($op_name,$this->getById($id)->toArray()['name']);
        }
        if ($this->destroy($ids)){
            $params=[
                Config::get('op_type')[2],
                Config::get('op_module')[3],
                Config::get('op_text')[4],
                implode(",", $op_name)
            ];
            tag('logs_db',$params);
            return op_success();
        }
        return op_fail();
    }

}